package com.example.Polymorphic;

public class Test3Polymorphic {

    public static void main(String[] args) {
        //useDog(new Dog());
        //useCat(new Cat());

        useAnimal(new Dog());
        useAnimal(new Cat());
    }

    public static void useAnimal(Animal animal) {   // Animal animal = new Dog();

        animal.eat();
        // animal.watchHome();

        if(animal instanceof Dog){
            Dog dog = (Dog) animal;
            dog.watchHome();
        }

       // Dog dog = (Dog) animal;
       // dog.watchHome();        //ClassCastException    类型转换异常
    }

    /*public static void useDog(Dog dog) {
        dog.eat();
    }
    public static void useCat(Cat cat) {
        cat.eat();
    }*/
}

abstract class Animal {
    public abstract void eat();
}

class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("狗吃肉");
    }
    public void watchHome() {
        System.out.println("看家");
    }
}

class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}

